

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author : Jame
 * @date : 2024/7/15 下午 2:53
 */
public class AA {

    static Node root;
    static String filePath = "D:\\node.txt";
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        init();
        Runtime.getRuntime().addShutdownHook(new Thread(AA::write));
        while (true) {
            enquire();
            System.out.println("再来一把？");
            String again = scanner.nextLine();
            if ("n".equals(again)) {
                break;
            }
        }
    }


    static void enquire() {
        Node q = root;
        for (int i = 0; i < 10; i++) {
            System.out.println("问题" + (i + 1) + ":" + q.question);
            String answer = scanner.nextLine();
            Node current = q;
            if ("y".equals(answer)) {
                q = q.yes;
            } else {
                q = q.no;
            }
            if (q == null) {
                Node n = createQuestion(current, "y".equals(answer), i == 9);
                if (root == null) {
                    root = n;
                }
                q = n;
            }
            if (i == 9) {
                System.out.println("符合条件的有：" + q.answers);
                System.out.println("输入a添加答案，输入q退出");
                if ("a".equals(scanner.nextLine())) {
                    System.out.println("输入添加的答案");
                    String otherAnswer = scanner.nextLine();
                    q.answers.add(otherAnswer);
                    System.out.println("符合条件的有：" + q.answers);
                }
            }
        }
    }

    static Node createQuestion(Node q, boolean isYes, boolean isAnswer) {
        Node node;
        if (isYes) {
            node = q.yes;
        } else {
            node = q.no;
        }
        if (node == null) {
            node = new Node();
        }
        if (isAnswer) {
            System.out.println("添加答案");
            List<String> answers = node.answers;
            if (answers == null) {
                answers = new ArrayList<>();
                node.answers = answers;
            }
            answers.add(scanner.nextLine());
        } else {
            System.out.println("创建题目");
            node.question = scanner.nextLine();
        }
        if (isYes) {
            q.yes = node;
        } else {
            q.no = node;
        }
        return node;
    }


    static void init() {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
                Node node = new Node();
                node.question = "生活在地上？";
                root = node;
                write();
            }
            String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
            if (StringUtils.hasLength(content)) {
                root = JSON.parseObject(content, Node.class);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    static void write() {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(JSON.toJSONString(root, true));
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class Node {
    public String question;
    public Node yes;
    public Node no;
    public List<String> answers;
}
